<?php
namespace hangpu8\hpcms\Utils\Builder;

use FormBuilder\Form;
use FormBuilder\Factory\Elm;

class TabForm
{
    // form对象
    private $builder;
    
    // Tab选项默认
    private $tabActive = 0;

    // Tab规则数据
    private $data = [];

    /**
     * 构造函数
     *
     * @param string $url
     */
    public function __construct($url = '')
    {
        $this->data = [
            'config'            => [
                'apiurl'        => '',
                'method'        => 'POST',
            ],
            'formRule'          => [
                'default'       => '',
                'rule'          => []
            ]
        ];
        if (empty($url))
        {
            $request = request();
            $query = $request->request()['s'];
            $url = "{$query}";
            $this->data['config']['apiurl'] = $url;
        }
        $this->builder = Form::elm($url,[],[]);
    }

    /**
     * 添加Tab表单
     *
     * @param string $field
     * @param string $title
     * @param object $callback
     * @return $this
     */
    public function addTabGroup(string $field,string $title,object $callback)
    {
        $rule = [
            'name'              => $field,
            'title'             => $title,
            'formRule'          => $callback($this),
        ];
        array_push($this->data['formRule']['rule'],$rule);
        return $this;
    }

    /**
     * 获取Tab子form规则
     *
     * @return array
     */
    public function getTabRule()
    {
        return $this->builder->formRule();
    }

    /**
     * 添加表单行
     *
     * @param string $field 组件字段
     * @param string $type 组件类型
     * @param string $title 组件标题
     * @param string $value 组件默认值
     * @param array $extend 扩展数据
     * @return $this
     */
    public function addRow(string $field,string $type,string $title,string $value = '', array $extend = [])
    {
        $component = Elm::$type($field,$title,$value);
        if ($extend)
        {
            foreach ($extend as $componentType => $componentTypeValue)
            {
                $component->$componentType($componentTypeValue);
            }
        }
        $this->builder->append($component);
        return $this;
    }

    /**
     * 设置行数据
     *
     * @param array $data 表单数据
     * @return $this
     */
    public function setFormData(array $data)
    {
        $this->builder->formData($data);
        return $this;
    }

    /**
     * 设置请求方式
     *
     * @param [type] $method
     * @return $this
     */
    public function setMethod($method = 'GET')
    {
        $this->data['config']['method'] = $method;
        return $this;
    }

    /**
     * 设置配置项
     *
     * @param [type] $name
     * @param [type] $value
     * @return $this
     */
    public function setConfig($name,$value)
    {
        $this->data['config'][$name] = $value;
        return $this;
    }

    /**
     * 快速生成表单
     *
     * @return array
     */
    public function create()
    {
        $formRule = $this->data['formRule']['rule'];
        $this->data['formRule']['default'] = $formRule[$this->tabActive]['name'];
        return $this->data;
    }

    /**
     * 获取builder生成类对象
     *
     * @return $this
     */
    public function builder()
    {
        return $this->builder;
    }
}